'use strict';
const Service = require('egg').Service;
let config = {};

if (process.env.NODE_ENV === 'development') {
  config = require('./dev_config');
} else if (process.env.NODE_ENV === 'production') {
  config = require('./prd_config');
}

class CMSService extends Service {
  // 获取banner信息
  async getBanner(data) {
    const ctx = this.ctx;
    let result = null;
    try {
      result = await ctx.curl(`${config.baseHost}:${config.basePort}${config.mockBaseUrl}/banner/page`, {
        method: 'GET',
        contentType: 'json',
        data,
        dataType: 'json',
      });

      return result.data.data.rows;
    } catch (error) {
      console.log(error);
      return [];
    }
  }

  // 获取menu信息
  async getMenu() {
    const ctx = this.ctx;
    let result = null;
    try {
      result = await ctx.curl(`${config.baseHost}:${config.basePort}${config.mockBaseUrl}/menu/tree`, {
        method: 'GET',
        dataType: 'json',
      });
      ctx.app.menus = result.data.data;
    } catch (error) {
      console.log(error);
      return [];
    }
  }

  // 获取列表信息
  async getContentList(menuId) {
    const ctx = this.ctx;
    let result = null;
    try {
      result = await ctx.curl(`${config.baseHost}:${config.basePort}${config.mockBaseUrl}/content/menu/page`, {
        method: 'GET',
        contentType: 'json',
        data: {
          menuId,
          page: ctx.query.page ? ctx.query.page : 1,
        },
        dataType: 'json',
      });

      return result.data.data.data;
    } catch (error) {
      console.log(error);
      return [];
    }
  }
  // 获取列表信息
  async getContentDetail() {
    const ctx = this.ctx;
    let result = null;
    try {
      result = await ctx.curl(`${config.baseHost}:${config.basePort}${config.mockBaseUrl}/content/get?id=${ctx.query.detailId}`, {
        method: 'GET',
        contentType: 'json',
        dataType: 'json',
      });
      return result.data.data;
    } catch (error) {
      console.log(error);
      return {};
    }
  }
}
module.exports = CMSService;
